#include <malloc.h>
#include <stdlib.h>
#include <math.h>

#define FAILURE 0
#define SUCCESS 1
#define OK 2

typedef int Status;
typedef float ElemSet;
typedef ElemSet* Plural;
//***********¸´ÊýÀàÐÍ--Á´Ê½´¢´æ/复数类型--链式储存****************
Status InitPlural(Plural *A,ElemSet v1,ElemSet v2){
	*A=(ElemSet *)malloc(2*sizeof(ElemSet));
	if(*A==NULL) return FAILURE;
	(*A)[0]=v1;
	(*A)[1]=v2;
	return SUCCESS;
}

int DestroyPlural(Plural *A){
	if(*A==NULL) return FAILURE;
	free(*A);
	return SUCCESS;
}

char* ToString(Plural *A){
	static char str[10];
	sprintf(str,"(%.1f%+.1fi)",(*A)[0],(*A)[1]);
	return str;
}

ElemSet GetRealPart(Plural* A){
	return (*A)[0];
}

ElemSet GetImagPart(Plural* A){
	return (*A)[1];
}

Status FillRealPart(Plural* A,ElemSet v){
	(*A)[0]=v;
	return OK;
}

Status FillImagPart(Plural* A,ElemSet v){
	(*A)[1]=v;
	return OK;
}

int Addition(Plural* A,Plural* B,Plural *Res){
	//(*Res)[0]=(*A)[0]+(*B)[0];
	FillRealPart(Res,GetRealPart(A)+GetRealPart(B));
	//(*Res)[1]=(*A)[1]+(*B)[1];
	FillImagPart(Res,GetImagPart(A)+GetImagPart(B));
	return SUCCESS;
}

int Subtraction(Plural* A,Plural* B,Plural* Res){
	//(*Res)[0]=(*A)[0]-(*B)[0];
	FillRealPart(Res,GetRealPart(A)-GetRealPart(B));
	//(*Res)[1]=(*A)[1]-(*B)[1];
	FillImagPart(Res,GetImagPart(A)-GetImagPart(B));
	return SUCCESS;
}

int Multiplication(Plural* A,Plural* B,Plural* Res){
	FillRealPart(Res,GetRealPart(A)*GetRealPart(B)-GetImagPart(A)*GetImagPart(B));
	FillImagPart(Res,GetRealPart(A)*GetImagPart(B)+GetRealPart(B)*GetImagPart(A));
	return SUCCESS;
	
}

int Devision(Plural *A,Plural *B,Plural *Res){	
	FillRealPart(Res,GetRealPart(A)*GetRealPart(B)+GetImagPart(A)*GetImagPart(B));
	FillImagPart(Res,GetRealPart(B)*GetImagPart(A)+GetRealPart(A)*GetImagPart(B));
	return SUCCESS;
}

float OneLength(Plural *A){	
	ElemSet len=GetRealPart(A)*GetRealPart(A)+GetImagPart(A)*GetImagPart(A);
	return sqrt(len);
}
